package guoan.repository;

import java.util.Date;
import java.util.List;

import guoan.domain.Exam;

import org.joda.time.DateTime;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ExamRepository extends PagingAndSortingRepository<Exam, Long>, JpaSpecificationExecutor<Exam> {

	Exam findByResTimeLessThanEqual(Date date);

	Exam findByIdAndResTimeLessThanAndExamTimeGreaterThanEqual(Long examId, DateTime nowTime, DateTime dateTime);

	List<Exam> findByResTimeLessThanAndExamTimeGreaterThanEqual(DateTime nowTime, DateTime dateTime);

	Exam findByIdAndResTimeLessThan(Long examId, DateTime nowTime);

	@Modifying
	@Query(value = "DELETE FROM exam WHERE date(exam_time) < date(now())", nativeQuery = true)
	void removeLast();

}
